<?php
/**
 * @name /www/EditUser.php
 * @author
 * @version 2011.03.30
 *
 * This is the page for Editing User Information by the Administrator.
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');

// Libraries
ReqLib('/lib/private/User.php');


function DefineHeader() {
?>

<script src="jquery.js" type="text/javascript" language="javascript"></script>
<script language="javascript">

//<!---------------------------------+
//  Developed by Roshan Bhattarai
//  Visit http://roshanbh.com.np for this script and more.
//  This notice MUST stay intact for legal use
// --------------------------------->

$(document).ready(function()
{
	$("#UserName").blur(function()
	{
		$('#Action')[0].disabled = true;
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		//check the username exists or not from ajax
		$.post("UserAvailability.php",{ UserName:$(this).val() } ,function(data)
        {
		  if(data=='no') //if username not avaiable
		  {
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{
			  //add message and change the class of the box and start fading
			  $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
			});
          }
		  else
		  {
		  $('#Action')[0].disabled = false;
		  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{
			  //add message and change the class of the box and start fading
			  $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
			});
		  }

        });

	});

	$("#Password").blur(function()
	{
		$('#Action')[0].disabled = true;

		$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");

		if($("#Password").val() == $("#ReTypePassword").val())
		{
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{
			  //add message and change the class of the box and start fading
			  $(this).html('Password Matches').addClass('messageboxerror').fadeTo(900,1);
			});

			$('#Action')[0].disabled = false;
		}

		else if($("#Password").val()== 0 && $("#ReTypePassword").val() == 0)
		{
			$('#Action')[0].disabled = false;
		}
		else
		{
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{
			  //add message and change the class of the box and start fading
			  $(this).html('Password Doesn\'t Match').addClass('messageboxerror').fadeTo(900,1);
			});
		}

	});

	$("#ReTypePassword").blur(function()
	{
		$('#Action')[0].disabled = true;

		$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");

		if($("#Password").val() == $("#ReTypePassword").val())
		{
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{
			  //add message and change the class of the box and start fading
			  $(this).html('Password Matches').addClass('messageboxerror').fadeTo(900,1);
			});

			$('#Action')[0].disabled = false;
		}

		else if($("#Password").val()== 0 && $("#ReTypePassword").val() == 0)
		{
			$('#Action')[0].disabled = false;
		}
		else
		{
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{
			  //add message and change the class of the box and start fading
			  $(this).html('Password Doesn\'t Match').addClass('messageboxerror').fadeTo(900,1);
			});
		}

	});

});
</script>

<script type="text/javascript">
  function onModifyRoles(select, value, text) {
    $roles = $('#RolesSelect Option');
    var text = '';
    $.each($roles, function(i, role) {
      text += (i ? '|' : '') + role.value;
    });
    $('#RolesHidden').val(text);
  }

  $(function() {
    $('#RolesSelect').selectList({
      onAdd: onModifyRoles,
      onRemove: onModifyRoles
    });
  });
</script>

<style type="text/css">

.messagebox{
	position:absolute;
	width:100px;
	margin-left:30px;
	border:1px solid #c93;
	background:#ffc;
	padding:3px;
}
.messageboxok{
	position:absolute;
	width:auto;
	margin-left:30px;
	border:1px solid #349534;
	background:#C9FFCA;
	padding:3px;
	font-weight:bold;
	color:#008000;

}
.messageboxerror{
	position:absolute;
	width:auto;
	margin-left:30px;
	border:1px solid #CC0000;
	background:#F7CBCA;
	padding:3px;
	font-weight:bold;
	color:#CC0000;
}

</style>

<?
}

/**
 * Defines page content.
 *
 */
function DefineContent() {
  // Page content goes here.
  ?>
<p>
  <a href="<?=_webroot('/') ?>">Home</a>.
</p>
<?
	if($_SESSION['userId'] >0){
?>
		<p>
		  <a href="<?=_webroot('/account.php') ?>">My Account</a>.
		</p>
		<p>
		  <a href="<?=_webroot('/logout.php') ?>">Logout</a>.
		</p>
    <form  action = "userRequestHandle.php" method="POST">
<center><table border=2>
	<tbody>
<?
	$u = new User();
	$userId = $_SESSION['userId'];

	//print($userId);/

	$u->Load($userId);

	//print_r($u);
?>

<?
	print('<input type="hidden" name="UserId" value="'.$u->UserId.'" />');
	print('<tr><td><div>User Name : </td><td><input name="UserName" type="text" id="UserName" value="" maxlength="15" /><span id="msgbox" style="display:none"></span></div></td></tr>');
	print('<tr><td><div>New Password : </td><td><input name="Password" type="password" id="Password" value="" maxlength="15" /><span id="msgbox" style="display:none"></span></div></td></tr>');
	print('<tr><td><div>Retype Password : </td><td><input name="ReTypePassword" type="password" id="ReTypePassword" value="" maxlength="15" /><span id="msgbox" style="display:none"></span></div></td></tr>');
	print('</tbody></table>');
  	print('<input type="submit" id="Action" name="Action" value="Update Login Info" />');
	print('<input type=button value="Back" onClick="history.go(-1)" />');
	print('</center>');
	print('<input type="hidden" name="Roles" id="RolesHidden" value="' . implode('|', $u->_Roles) . '" />');

  	print('</form>');
	}
   else{
?>
		<p>
		  <a href="<?=_webroot('/login_register.php') ?>">Login/Register</a>.
		</p>
<?
   }
}

// Launches the page; without this, nothing happens.
require_once('_loader.php');